from matplotlib import pyplot as plt
import numpy as np

#初始设置，设置俩类数据
num = 30
x1i = np.linspace(0, 6, num)
y1i = -4 * x1i + 6 + np.random.uniform(-2, 3, num)

# y = -4x + 4
plt.figure()
plt.scatter(x1i, y1i, color='b')

x2i = np.linspace(0, 6, num)
y2i = -4 * x2i - 2 + np.random.uniform(-2, 3, num)
plt.scatter(x2i, y2i, color='r')

c1 = np.zeros([num, 2])
c2 = np.zeros([num, 2])
for i in range(num):
    c1[i][0] = x1i[i]
    c1[i][1] = y1i[i]
    c2[i][0] = x2i[i]
    c2[i][1] = y2i[i]

#计算类间散度矩阵
#计算类1的样本平均
m1 = np.array([[np.sum(x1i) / num, np.sum(y1i) / num]])
m2 = np.array([[np.sum(x2i) / num, np.sum(y2i) / num]])

#m0 = (m1 + m2) / 2
sb = np.matmul(m1.T, m2)

#类内散度矩阵
s1 = np.zeros([2,2])
s2 = np.zeros([2,2])
for i in range(num):
    s1 = s1 + np.matmul(c1[i].T - m1, (c1[i].T - m1).T)
    s2 = s2 + np.matmul(c2[i].T - m1, (c2[i].T - m1).T)
sw = s1 + s2

res = np.matmul(np.linalg.inv(sw), sb)

#plt.show()